package com.ABdolphin.forum.controller;

import com.ABdolphin.forum.common.AppConfig;
import com.ABdolphin.forum.common.AppResult;
import com.ABdolphin.forum.common.ResultCode;
import com.ABdolphin.forum.model.Article;
import com.ABdolphin.forum.model.ArticleReply;
import com.ABdolphin.forum.model.User;
import com.ABdolphin.forum.services.IArticleReplyService;
import com.ABdolphin.forum.services.IArticleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Tag(name = "回复接口")
@RestController
@RequestMapping("/reply")
public class ArticleReplyController {
    @Resource
    private IArticleService articleService;
    @Resource
    private IArticleReplyService articleReplyService;
    @Operation(summary = "回复帖子")
    @PostMapping("/create")
    public AppResult create(HttpServletRequest request,
                            @Parameter(name = "articleId",description = "帖子Id")@RequestParam("articleId")@NotNull Long articleId,
                            @Parameter(name = "content",description = "回复内容")@RequestParam("content")@NotBlank String content){
        //1,用户状态2,帖子状态
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        if (user.getState()==1){
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        Article article=articleService.selectById(articleId);
        if (article==null||article.getDeleteState()==1){
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        if (article.getState()==1){
            return AppResult.failed(ResultCode.FAILED_ARTICLE_BANNED);
        }
        ArticleReply articleReply=new ArticleReply();
        articleReply.setContent(content);
        articleReply.setArticleId(articleId);
        articleReply.setPostUserId(user.getId());
        articleReplyService.create(articleReply);
        return AppResult.success();
    }
    @Operation(summary = "获取回复列表")
    @GetMapping("/getReplies")
    public AppResult<List<ArticleReply>> getRepliesByArticleId(@Parameter(name = "articleId" ,description = "帖子id")
                                                                   @RequestParam("articleId") Long articleId){
        Article article=articleService.selectById(articleId);
        if (article==null||article.getDeleteState()==1){
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        if (article.getState()==1){
            return AppResult.failed(ResultCode.FAILED_ARTICLE_BANNED);
        }
        List<ArticleReply> articleReplies = articleReplyService.selectByArticleId(articleId);
        return AppResult.success(articleReplies);
    }
}















